"=============================================================================
" File: simplelife_easycopy.vim
" Author: GOVO Li<ejialin@gmail.com>
" Last Change: 10-Jul-2011.
" Version: 0.1
" WebPage: http://guitarbean.com/simplelife_easycopy-vim
" Description: vim plugins for Ctrl-c copy,Ctrl-v paste,and Ctrl-s save.
" SeeAlso: http://code.google.com/
" Usage:
" Copy to plugin


"记录上一次browse()打开的记录
let g:Browse_File_gui_lastDir=getcwd()
"通过把最后一个斜杠（或反斜杠）及之后的字符剔除的简单判断方式，
"返回文件所在文件完整目录
"如 D:\myfoder\file.txt = D:\myfoder
"&  D:\file.txt         = D:
"函数仅对字符串进行分析，不会检查文件是否真实存在，因此也不会判断最后一个字符是否文件名
"如 D:\myfoder\myfoder2 = D:\myfoder  把myfoder2当作文件名了
"但 D:\myfoder\myfoder2\= D:\myfoder\myfoder
function! Browse_File_gui_setLastDir(file)
    let l:par=max([strridx(a:file,"/"),strridx(a:file,"\\")])
    if l:par>-1
        let l:dir=strpart(a:file,0,l:par)
    endif
    if l:par==-1 || l:dir==""
        let l:dir=getcwd()
    endif
    return l:dir 
endfunction
"浏览文件，如果 save=1，则保存文件，如果当前buff没有指定名字，则会
"弹出浏览框，你可以选择目录和指定文件名来保存；如果buff指定有名字，
"则以buff名字保存至当前目录。
"如果 save=0，则弹出浏览框，选择指定文件来打开。
function! Browse_File_gui(save)
    let l:save=a:save || 0
    let l:title="File Browser"
    let l:dir=""
    let l:name=""
    let l:nameTemp="newfile-"
    if l:save!=0
        let l:bname=bufname("%")
        if l:bname==""
            let l:found=1
            let l:count=1
            let l:dir=getcwd()
            let l:par=max([strridx(l:dir,"/"),strridx(l:dir,"\\")])
            if l:par==len(l:dir)-1
                let l:dirFix=l:dir
            else
                let l:dirFix=l:dir."\\"
            endif
            while l:found
                let l:name=l:nameTemp.l:count
                if findfile(l:dirFix.l:name)==""
                    let l:found=0
                    break
                endif
                let l:count=l:count+1
            endwhile
            let l:selectfile=browse(l:save,l:title,l:dir,l:name)
            if l:selectfile==""
                return
            endif
            exe "w"l:selectfile
            let g:Browse_File_gui_lastDir=Browse_File_gui_setLastDir(l:selectfile)
        else
            exe "update" l:bname
        endif
    else
        let l:selectfile=browse(l:save,l:title,l:dir,l:name)
        if filereadable(l:selectfile)
            exe "e"l:selectfile
            let g:Browse_File_gui_lastDir=Browse_File_gui_setLastDir(l:selectfile)
        else
            echo "file not found"
        endif
    endif
    return 
endfunction


""为了在粘贴时无名寄存器的还原
"let g:Paste_ctrl_v_reg_temp_val=''
"inoremap <C-v> <C-R> <Space><Esc>:let g:Paste_ctrl_v_reg_temp_val=getreg('""')<cr>v"*p<Esc>:let @"=g:Paste_ctrl_v_reg_temp_val<cr>a

""noremap <C-s> :w!<cr>
""inoremap <C-s> <Esc>:w!<cr>a
"noremap <C-s> :call Browse_File_gui(1)<cr>
"inoremap <C-s> <Esc>:call Browse_File_gui(1)<cr>a
"nnoremap <C-v> "*p
"vnoremap <C-v> "_d"*P
""inoremap <C-v> <C-R> <Space><Esc>v"*pa
"noremap <C-c> "*y
"inoremap <C-c> <Esc>"*y
"noremap <C-z> u
"inoremap <C-z> <Esc>ui
"inoremap <C-r> <Esc><C-r>i

"inoremap <C-w> <Esc>:q<cr>
"inoremap <C-w> <Nop>

map <MiddleMouse> <Esc>
map <2-MiddleMouse> <Nop>
map <3-MiddleMouse> <Nop>
map <4-MiddleMouse> <Nop>

imap <MiddleMouse> <Nop>
imap <2-MiddleMouse> <Nop>
imap <3-MiddleMouse> <Nop>
imap <4-MiddleMouse> <Nop>

noremap <MiddleMouse> <Esc>
inoremap <MiddleMouse> <Esc>
cnoremap <MiddleMouse> <Esc>
onoremap <MiddleMouse> <Esc>

" the local host setting
"let g:OpenInternetBrowser_setting={
    "\'梦幻西游':['D:\work\Building\','http://localhost.163.com/'],
    "\'itown':['D:\work\Building\site_itown\itown_all\','http://localitown.163.com/'],
    "\'sg':['D:\work\Building\site_sg\','http://localsg.163.com/'],
    "\'dt':['D:\work\Building\site_dt\dt_all\','http://localdt.163.com/']
    "\}
function! OpenInternetBrowser_select_site(...)
    let l:count=1
    let l:dict={}
    let l:txt=''
    let l:sitevalue=[]
    for [l:key,l:value] in items(g:OpenInternetBrowser_setting)
        let l:txt.=string(l:count).': '.l:key.' ['.l:value[1]. ' , '.l:value[0]."]\n"
        call add(l:value,iconv(l:key,'UTF-8',&enc))
        let l:dict[l:count]=l:value
        let l:count+=1
    endfor
    let l:txt="please set the current site:\n".l:txt
    let l:txt=iconv(l:txt,"UTF-8",&enc)
    if a:0>0 && a:1
        let l:site=a:1
    else
        let l:site=input(l:txt)
    endif
    let l:site=substitute(l:site,'^\s\+','','g')
    let l:site=substitute(l:site,'\s\+$','','g')
    if has_key(l:dict,l:site)
        let l:sitevalue=l:dict[l:site]
        let g:OpenInternetBrowser_host=l:sitevalue[1]
        let g:OpenInternetBrowser_host_path=l:sitevalue[0]
        echohl WarningMsg
        echo l:sitevalue[2] l:sitevalue[1] ',' l:sitevalue[0]
        echohl None
    endif
    return 
endfunction

"the local host url
let g:OpenInternetBrowser_host='http://local.163.com/'
"the host path
let g:OpenInternetBrowser_host_path='D:\webapp\'

function OpenInternetBrowser_set_default_host()
    let l:count=0
    for [l:key,l:value] in items(g:OpenInternetBrowser_setting)
        if l:count==0
            let g:OpenInternetBrowser_host=l:value[1]
            let g:OpenInternetBrowser_host_path=l:value[0]
            break
        endif
    endfor
endfunction
call OpenInternetBrowser_set_default_host()

"use browser to view the url or current file
"if browser is "", run default browser
"if browser is not "",run the browser
"if nohost is 1, stop using host
function! OpenInternetBrowser(browser,nohost)
    let l:bfname=bufname("%")
    if l:bfname==""
        return
    endif
    let l:dir=getcwd()
    let l:par=max([strridx(l:dir,"/"),strridx(l:dir,"\\")])
    if l:par==len(l:dir)-1
        let l:dirFix=l:dir
    else
        let l:dirFix=l:dir."\\"
    endif
    let l:file=l:dirFix .l:bfname
    
    if stridx(l:file,g:OpenInternetBrowser_host_path)==0 && a:nohost==""
        let l:url=strpart(l:file,strlen(g:OpenInternetBrowser_host_path))
        let l:url=g:OpenInternetBrowser_host .substitute(l:url,"\\\\","\/","g")
    else
        let l:url=l:file
    endif
    if a:browser==""
        exe 'silent !start cmd /c start "" "'.l:url.'"'
    else
        exe 'silent !start' a:browser '"'.l:url.'"'
    endif
endfunction
"run explorer to view the working dir
function! OpenExplorer(dir)
    if a:dir==""
        let l:dir=getcwd()
    else
        let l:dir=a:dir
    endif
    exe 'silent !start cmd /c explorer.exe' l:dir
endfunction

noremap <silent><F12> :call OpenInternetBrowser("","")<cr>
inoremap <silent><F12> <C-O>:call OpenInternetBrowser("","")<cr>
noremap <silent><leader>ff :call OpenInternetBrowser("E:\\Program Files\\Mozilla Firefox4\\firefox.exe","")<cr>
noremap <silent><leader>ch :call OpenInternetBrowser($HOME."\\Local Settings\\Application Data\\Google\\Chrome\\Application\\chrome.exe","")<cr>
noremap <silent><leader>ie :call OpenInternetBrowser("C:\\Program Files\\Internet Explorer\\iexplore.exe","")<cr>
noremap <silent><leader>op :call OpenInternetBrowser("E:\\Program Files\\Opera\\opera.exe","")<cr>
noremap <silent><leader>dw :call OpenInternetBrowser("E:\\Program Files\\Adobe\\Adobe Dreamweaver CS4\\Dreamweaver.exe",1)<cr>
noremap <silent><leader>ex :call OpenExplorer("")<cr>
noremap <silent><leader>es :call OpenInternetBrowser_select_site()<cr>

function! SetFileEncoding_select(...)
    if a:0>0 && a:1
        set fileencoding=a:1
        return
    endif
    "let l:encodelist= g:SetFileEncoding_list
    if !exists("g:SetFileEncoding_list")
        let l:encodelist=['utf8','gbk','ucs-bom','latin1']
    else
        let l:encodelist=g:SetFileEncoding_list
    endif
    let l:txt="Please select the fileencoding:\n"
    let l:encodeTxtList=[]
    let l:encodeDict={}
    let l:count=1
    for l:value in l:encodelist
        let l:temp=l:count.'.'.l:value
        let l:encodeDict[l:count]=l:value
        call add(l:encodeTxtList,l:temp)
        let l:count+=1
    endfor

    let l:txt.=join(l:encodeTxtList,', ')." :\n"
    let l:ipt=input(l:txt)
    let l:ipt=substitute(l:ipt,'^\s\+','','g')
    let l:ipt=substitute(l:ipt,'\s\+$','','g')
    if has_key(l:encodeDict,l:ipt)
        exe "set fileencoding=".l:encodeDict[l:ipt]
        echohl WarningMsg
        echo l:encodeDict[l:ipt]
        echohl None
    endif

endfunction
"noremap <silent><F11> :call SetFileEncoding_select()<cr>
"inoremap <silent><F11> <C-O>:call SetFileEncoding_select()<cr>
